package com.petalsgroup.petals.dao.user.impl;

import com.petalsgroup.petals.dao.user.MySelfDao;
import com.petalsgroup.petals.pojo.AnimationSimpleDO;
import com.petalsgroup.petals.pojo.UserDO;
import com.petalsgroup.petals.util.JDBCUtils;
import org.junit.Test;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;
import java.util.Map;
import java.util.Set;

public class MySelfDaoImpl implements MySelfDao {
    //使用数据库连接池
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public Boolean updateUser(UserDO user) {
        String sql = "update user set password = ? ,name = ? , email = ? where uid = ?";
        int update = template.update(sql, user.getPassword(), user.getName(), user.getEmail(), user.getUid());
        if (update==1){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public List<AnimationSimpleDO> findAidByUid(Integer uid) {
        String sql = "select aid from collect where uid = ?";
        List<AnimationSimpleDO> list = template.query(sql, new BeanPropertyRowMapper<>(AnimationSimpleDO.class), uid);
        return list;
    }

    @Override
    public UserDO findUidByUsername(String username) {
        String sql = "select * from user where username = ?";
        List<UserDO> list = template.query(sql, new BeanPropertyRowMapper<>(UserDO.class), username);
        return list.get(0);
    }

    @Override
    public Boolean findAttention(Integer uid, Integer uided) {
        String sql = "select count(*) from attention where uid = ? and uided = ?";
        Integer count = template.queryForObject(sql, Integer.class, uid, uided);
        if (count > 0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public void saveAttention(Integer uid, Integer uided) {
        String sql = "insert into attention (uid,uided) value (?,?)";
        template.update(sql,uid,uided);
    }

    @Override
    public Integer findAttentionNum(Integer uid) {
        String sql = "select count(*) from attention where uid = ?";
        Integer integer = template.queryForObject(sql, Integer.class, uid);

        return integer;
    }

}
